home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / LIBRARY / DSUTIL12 / UNIX2DOS / UNIX2DOS.PAS < prev   
Pascal/Delphi Source File  |  1993-09-20  |  12KB  |  379 lines

  1. {-----------------------------------------------------------------------}
  2. { PROJECT        NON-PROFIT HIGH QUALITY PROFESSIONAL SOFTWARE,  }
  3. {            AVAILABLE FOR ALL WORLD                }
  4. { LIBRARY        SYSTEM UTILITIES                                }
  5. { MODULE        CONVERT_UNIX_TO_DOS_TEXT                        }
  6. { FILE NAME        UNIX2DOS.PAS                    }
  7. { PURPOSE               CONVERT UNIX-FORMATTED STYLE DOCUMENTATION TO   }
  8. {                       DOS-FORMATTED STYLE DOCUMENTATION               }
  9. { VERSION        1.10                        }
  10. { DATE            20-Sep-93                    }
  11. { DESIGN        Dmitry Stefankov                }
  12. { IMPLEMENTATION    Dmitry Stefankov                 }
  13. { COMPANY        Freelance Software Engineer            }
  14. { ADDRESS        Isakowskogo str, 4-2-30                }
  15. {            Moscow, 123181                    }
  16. {            USSR                        }
  17. {            Tel. 007 (095) 944-6304                }
  18. { COPYRIGHT NOTICE    Copyright (C) 1987-1993, Dmitry Stefankov    }
  19. { RESTRICTED RIGHTS    AVAILABLE ONLY FOR FREE DISTRIBUTION,           }
  20. {            NOT FOR COMMERCIAL PURPOSE            }
  21. { COMPUTER        IBM PC or compatible                }
  22. { OPERATING SYSTEM    MS/PC-DOS Version 3.30 or higher        }
  23. { COMPILER        Turbo Pascal Version 6.0            }
  24. {                       (Borland International Inc.)  or compatible     }
  25. { ASSEMBLY LANGUAGE    Microsoft MASM 5.10 or compatible               }
  26. { LINKER        Turbo Pascal internal                           }
  27. { ARGUMENTS             <infile>   -   input filename                   }
  28. {            <outfile>  -   output filename            }
  29. { RETURN        See error return codes definitions        }
  30. { REQUIRES        Source Code Files                               }
  31. {                       NONE                                            }
  32. {                       External Object Files                           }
  33. {                       NONE                                            }
  34. { NATURAL LANGUAGE      English Language                                }
  35. { SPECIAL        None                        }
  36. { DESCRIPTION        1.Read   input  stream                          }
  37. {                       2.Format output stream                          }
  38. {                       3.Write  output stream                          }
  39. { REVISION HISTORY    Dima Stefankov (DS)                }
  40. {               1.00  08-Jul-93  DS  initilal release        }
  41. {            1.01  13-Jul-93  DS  updated help screen    }
  42. {                       1.02  16-Jul-93  DS  fixed problem with string  }
  43. {                                            size                       }
  44. {            1.10  20-Sep-93  DS  some style updates        }
  45. {-----------------------------------------------------------------------}
  46.  
  47.  
  48. {*======================= PROGRAM HEADER PART ==========================*}
  49.  
  50. PROGRAM  UnixToDosTextReFormat;
  51.  
  52.  
  53. {*** other modules ***}
  54. {*USES;*}
  55.  
  56. {** switches for compilation **}
  57. {$S-}          {*  stack checking               *}
  58. {$R-}             {*  range checking               *}
  59. {$X-}             {*  extended syntax              *}
  60.  
  61.  
  62. {** debugging version **}
  63. {***$DEFINE DebugVersion}
  64.  
  65.  
  66. {*========================== CONSTANTS PART ============================*}
  67.  
  68. CONST
  69.    { program descriptions }
  70.      asPurpose                  =       'UNIX to DOS reformat ';
  71.      asMsgVersion               =       'Version ';
  72.      asVersion                  =       '1.10';
  73.      asAuthor                   =       'Dmitry Stefankov';
  74.      asCopyright                =       'Copyright (c) 1987, 1993';
  75.      asProgram                  =       'Unix2Dos';
  76.      asProgramPrompt            =       asProgram+': ';
  77.      asProgramU                 =       'UNIX2DOS';
  78.  
  79.  
  80.    { program exit codes }
  81.      errTerminateOK             =     0;
  82.      errBadParamStr             =     1;
  83.      errSourceNotFound          =     2;
  84.      errDestDontWrite           =     3;
  85.  
  86.    { Dos miscellaneous }
  87.      achDosSwitch               =     '/';
  88.      achUnixSwitch              =     '-';
  89.      achCommentMark             =     '#';
  90.      achDosEndFile              =     #$1A;
  91.      aDosFileNameLength         =     13;
  92.  
  93.    { TP error codes }
  94.      errOK                      =     0;
  95.  
  96.  
  97.    { base numeric system }
  98.      aHexRadix                  =     16;
  99.      aMaxByteVal                =     255;
  100.      achHexPrefix               =     '$';
  101.      achHexSuffix               =     'h';
  102.  
  103.    { character constants }
  104.      achNULL                    =     #0;
  105.      achHTAB                    =     #9;
  106.      achLF                      =     #10;
  107.      achFF                      =     #12;
  108.      achCR                      =     #13;
  109.      achESC                     =     #27;
  110.      achSPC                     =     #32;
  111.      achBlank                   =     ' ';
  112.      achColon                   =     ':';
  113.      achComma                   =     ',';
  114.      achSemiColon               =     ';';
  115.      achPoint                   =     '.';
  116.      achZERO                    =     '0';
  117.      achNine                    =     '9';
  118.      achUpperA                  =     'A';
  119.      achUpperF                  =     'F';
  120.      achLowerA                  =     'a';
  121.      achLowerF                  =     'f';
  122.      achDEL                     =     #127;
  123.  
  124.    { string constants }
  125.      asBlankStr                 =       '';
  126.      asSpaces2                  =       achBlank+achBlank;
  127.      asSpaces4                  =       asSpaces2+asSpaces2;
  128.      asSpaces6                  =       asSpaces4+asSpaces2;
  129.  
  130.  
  131.    { user confirm }
  132.      achUserWant                =     'Y';
  133.      achUserDontWant            =     'N';
  134.  
  135.    { strings definitions }
  136.      aMaxLinesPerPage           =      66;
  137.      aSkipLines                 =      3;
  138.      aLinesOnFirstPage          =      aMaxLinesPerPage - aSkipLines;
  139.  
  140.    { buffers size }
  141.      aMaxTextBufSize            =       4096;          { 4K  }
  142.  
  143.  
  144. {*==================== TYPE DECLARATIONS PART ==========================*}
  145.  
  146. TYPE
  147.     STR2        =       STRING[2];
  148.     STR4        =       STRING[4];
  149.     STR6        =       STRING[6];
  150.     STR8        =       STRING[8];
  151.     STR9        =       STRING[9];
  152.     STR10       =       STRING[10];
  153.     STR80       =       STRING[80];
  154.  
  155.  
  156. {*====================== TYPED CONSTANTS PART ==========================*}
  157.  
  158. CONST
  159.    gsInputFileName              :       STR80            =   asBlankStr;
  160.    gsOutputFileName             :       STR80            =   asBlankStr;
  161.  
  162.    gliLineCount                 :       System.Longint   =   0;
  163.    gdwLinesOnPage               :       System.Word      =   0;
  164.  
  165.  
  166. {*=========================== VARIABLES PART ===========================*}
  167.  
  168. VAR
  169.    gfInputStream                :       FILE  OF  System.Byte;
  170.    gfInputStreamRec             :       FILE ABSOLUTE gfInputStream;
  171.    gfOutputStream               :       FILE  OF  System.Byte;
  172.    gfOutputStreamRec            :       FILE ABSOLUTE gfOutputStream;
  173.  
  174.    gdwBytesInLine               :       System.Word;
  175.    gsTempInput                  :       STRING;
  176.    giErrorCode                  :       System.Integer;
  177.    gdbIndex                     :       System.Byte;
  178.    gdbTextBuf                   :       ARRAY[1..aMaxTextBufSize] OF System.Byte;
  179.  
  180.  
  181. {*=========================== FUNCTIONAL PART ==========================*}
  182.  
  183. FUNCTION  _fnbFileExist(VAR fStruc : FILE; sFileName : STRING) : System.Boolean;
  184. {* Check that file exits. *}
  185. VAR
  186.   bResult  :  System.Boolean;
  187.  
  188. BEGIN
  189.   {** try to open the file **}
  190.   System.Assign(fStruc,sFileName);
  191.   {$I-}
  192.   System.Reset(fStruc);
  193.   {$I+}
  194.  
  195.   {** copy the result of last I/O operation **}
  196.   bResult := (System.IOResult = 0);
  197.  
  198.   IF (bResult)
  199.     THEN  System.Close(fStruc);
  200.   {if-then}
  201.  
  202.   _fnbFileExist := bResult;
  203. END; { _fnbFileExist }
  204.  
  205.  
  206. FUNCTION   _fnchGetFirstChar(sInput : STRING) : System.Char;
  207. {* Returns a first char from string. *}
  208. VAR
  209.   chTemp  :  System.Char;
  210.  
  211. BEGIN
  212.    IF (System.Length(sInput) <> 0)
  213.      THEN  chTemp := sInput[1]
  214.      ELSE  chTemp := achNULL;
  215.    {if-then-else}
  216.   _fnchGetFirstChar := chTemp;
  217. END;
  218. { _fnchGetFirstChar }
  219.  
  220.  
  221. {*=========================== PROCEDURAL PART ==========================*}
  222.  
  223. PROCEDURE  _ReadUnixLine;
  224. {* Reads an UNIX-file line. *}
  225. VAR
  226.   dbTemp        :       Byte;
  227.   bEndOfLine    :       System.Boolean;
  228.  
  229. BEGIN
  230.    gdwBytesInLine := 0;
  231.    dbTemp := $0;
  232.    bEndOfLine := System.False;
  233.  
  234.    WHILE NOT(bEndOfLine)
  235.          AND NOT(EOF(gfInputStream)) DO
  236.    BEGIN
  237.       System.Read(gfInputStream,dbTemp);
  238.       IF  (dbTemp <> System.Byte(achLF))
  239.        THEN  BEGIN
  240.           If (gdwBytesInLine <= (aMaxTextBufSize-1))
  241.             THEN BEGIN
  242.                 System.Inc(gdwBytesInLine);
  243.                 gdbTextBuf[gdwBytesInLine] := dbTemp;
  244.                 END;
  245.           {if-then}
  246.              END
  247.        ELSE  BEGIN
  248.           bEndOfLine := System.True;
  249.              END;
  250.       {if-then-else}
  251.    END;
  252.    {while-do}
  253. END;
  254. { _ReadUnixLine }
  255.  
  256.  
  257. PROCEDURE  _WriteDosLine;
  258. {* Writes a DOS-file line. *}
  259. VAR
  260.   dwIndex        :       System.Word;
  261.   dbTemp         :       System.Byte;
  262.  
  263. BEGIN
  264.    IF (gdwBytesInLine <> 0)
  265.      THEN  BEGIN
  266.         FOR  dwIndex := 1  TO  gdwBytesInLine  DO
  267.         BEGIN
  268.            System.Write(gfOutputStream,gdbTextBuf[dwIndex]);
  269.         END;
  270.         {for-to-do}
  271.           END;
  272.    {if-then}
  273.    dbTemp := System.Byte(achCR);
  274.    System.Write(gfOutputStream,dbTemp);
  275.    dbTemp := System.Byte(achLF);
  276.    System.Write(gfOutputStream,dbTemp);
  277. END;
  278. { _WriteDosLine }
  279.  
  280.  
  281.  
  282. PROCEDURE    _CopyrightDisplay;
  283. {* Outputs the copyright notice. *}
  284. BEGIN
  285.      System.WriteLn(asPurpose+
  286.                     asMsgVersion+
  287.                     asVersion+
  288.                     achComma+achBlank +
  289.                     asCopyright+
  290.                     asSpaces2+
  291.                     asAuthor);
  292. END;  { _CopyrightDisplay }
  293.  
  294.  
  295. {*============================== MAIN PART =============================*}
  296.  
  297. BEGIN
  298.     _CopyrightDisplay;
  299.  
  300.     IF (System.ParamCount < 2)
  301.       THEN  BEGIN
  302.          System.WriteLn(asProgramPrompt+'short help for you.');
  303.          System.WriteLn('Usage: '+ asProgram + ' infile outfile');
  304.          System.WriteLn('  infile   -  source file (UNIX-style  text)');
  305.          System.WriteLn('  outfile  -  target file (MSDOS-style text)'); 
  306.          System.Halt(errBadParamStr);
  307.             END;
  308.     {if-then}
  309.  
  310.     gsInputFileName  := System.ParamStr(1);
  311.     gsOutputFileName := System.ParamStr(2);
  312.  
  313.  
  314.     {** source file exists? **}
  315.     IF  NOT(_fnbFileExist(gfInputStreamRec,gsInputFileName)) THEN
  316.     BEGIN
  317.       System.WriteLn(asProgramPrompt+'Unable to open file '+gsInputFileName);
  318.       System.Halt(errSourceNotFound);
  319.     END;
  320.     {if-then}
  321.  
  322.     {** destination file present? **}
  323.     IF (_fnbFileExist(gfOutputStreamRec,gsOutputFileName)) THEN
  324.     BEGIN
  325.       System.Write(asProgramPrompt+'Output file '+gsOutputFileName+
  326.                    ' already exists. Overwrite? (n/y): ');
  327.       System.ReadLn(gsTempInput);
  328.       IF (System.UpCase(_fnchGetFirstChar(gsTempInput)) <> achUserWant)
  329.         THEN  System.Halt(errDestDontWrite);
  330.       {if-then}
  331.     END;
  332.     {if-then}
  333.  
  334.   {* open input stream *}
  335.     System.Assign(gfInputStream,gsInputFileName);
  336.     System.Reset(gfInputStream);
  337.  
  338.   {* open output stream *}
  339.     System.Assign(gfOutputStream,gsOutputFileName);
  340.     System.ReWrite(gfOutputStream);
  341.  
  342.  
  343.   {* first page length = 63 lines: just copy *}
  344.     FOR  gdbIndex := 1  TO  aLinesOnFirstPage  DO
  345.     BEGIN
  346.        System.Inc(gliLineCount);
  347.        System.Write(achCR+asProgramPrompt+'Line ',gliLineCount);
  348.        _ReadUnixLine;
  349.        _WriteDosLine;
  350.     END;
  351.  
  352.  
  353.   {* all other pages = 66 lines *}
  354.     WHILE  NOT(System.EOF(gfInputStream))  DO
  355.     BEGIN
  356.        System.Inc(gliLineCount);
  357.        IF  (gdwLinesOnPage > aMaxLinesPerPage)
  358.          THEN  gdwLinesOnPage := 0;
  359.        {if-then}
  360.        System.Inc(gdwLinesOnPage);
  361.        System.Write(achCR+asProgramPrompt+'Line ',gliLineCount);
  362.        _ReadUnixLine;
  363.        IF (gdwLinesOnPage > aSkipLines)
  364.          THEN  _WriteDosLine;
  365.        {if-then}
  366.     END;
  367.     {while-do}
  368.  
  369.  
  370.  {* close all streams *}
  371.     System.Close(gfInputStream);
  372.     System.Close(gfOutputStream);
  373.  
  374.  
  375.   {* last report *}
  376.     System.WriteLn;
  377.     System.WriteLn(asProgramPrompt+'Done.');
  378.  
  379. END.